### REPLICATION FILE -- Analysis
### Lawrence Ezrow, Margit Tavits & Jonathan Homola
### "Voter Polarization, Strength of Partisanship, 
### and Support for Extremist Parties"
### Comparative Political Studies 47: 1558-1583
### http://dx.doi.org/10.1177/0010414013512605

## clear environment, set seed, install/load packages
rm(list=ls())
set.seed(12435); options(stringsAsFactors=F)
#install.packages("foreign"); install.packages("xtable"); install.packages("psych")
#install.packages("sandwich"); install.packages("stargazer"); install.packages("lmtest")
library("foreign"); library("xtable"); library("psych")
library("sandwich"); library("stargazer"); library("lmtest")

## function to calculate STATA-style clustered standard errors
cl <- function(dat,fm, cluster){
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL)}

## set working directory
#setwd(" ... ")

## read in the main dataset 
cps <- read.dta("EzrowTavitsHomola_PolarizationExtremism_Data.dta")
cps_pc <- subset(cps, postcom==1)
cps_ad <- subset(cps, advind==1)


####
#### Table 1 Descriptive Statistics for the Dependent 
#### Variable (Vote Share) and the Independent Variables
####
xtable(describe(cps[,c("avs", "dist", "pid", "mean_party_pro_elect",
                       "avg_age1", "age_dem", "mv_stddev")])[,c(3,4)], digits=2)

xtable(describe(cps_pc[,c("avs", "dist", "pid", "mean_party_pro_elect",
                          "avg_age1", "age_dem", "mv_stddev")])[,c(3,4)], digits=2)

xtable(describe(cps_ad[,c("avs", "dist", "pid", "mean_party_pro_elect",
                          "avg_age1", "age_dem", "mv_stddev")])[,c(3,4)], digits=2)



####
#### Table 2 The Conditional Effect of Voter 
#### Polarization on Parties' Vote Shares
####
mod1 <- lm(avs ~ dist + distxmv_std + mv_stddev + nop5, data=cps)
mod2 <- lm(avs ~ dist + distxmv_std + mv_stddev + nop5, data=cps_pc)
mod3 <- lm(avs ~ dist + distxmv_std + mv_stddev + nop5, data=cps_ad)
mod4 <- lm(avs ~ dist + distxmv_std + mv_stddev + nop5 + postcom + 
             postcomxdist + postcomxmv_std + distxmv_stdxpostcom, data=cps)
mod5 <- lm(avs ~ dist + mv_stddev + age_dem + distxmv_std + 
             distxage_dem + age_demxmv_std + distxage_demxmv_std + 
             nop5, data=cps)

## clustered SEs
mod1 <- cl(cps, mod1, cps$electid)
mod2 <- cl(cps_pc, mod2, cps_pc$electid)
mod3 <- cl(cps_ad, mod3, cps_ad$electid)
mod4 <- cl(cps, mod4, cps$electid)
mod5 <- cl(cps, mod5, cps$electid)

stargazer(mod1, mod2, mod3, mod4, mod5,
          omit.stat=c("f", "ser", "bic", "ll", "adj.rsq"), 
          star.cutoffs = c(0.1, 0.05, 0.01),
          column.sep.width="1pt", digits=2,
          dep.var.labels.include = FALSE,
          dep.var.caption="Outcome variable: Vote Share")



####
#### Table 3 The Conditioning Effects of Partisanship 
#### as Measured by Levels of Partisan Identification, 
#### Voter Familiarity, and Party Age
####
mod1 <- lm(avs ~ dist + mv_stddev + pid + distxmv_std + 
             pidxmv_std + distxpid + distxpidxmv_std + nop5, data=cps)
mod2 <- lm(avs ~ dist + mv_stddev + mean_party_pro_country + 
             distxmv_std + mean_party_proxmv_std_c + distxmean_party_pro_c + 
             distxmean_party_proxmv_std_c + nop5, data=cps)
mod3 <- lm(avs ~ dist + mv_stddev + avg_age1 + distxmv_std + 
             avg_age1xmv_std + distxage1 + distxavg_age1xmv_std + nop5, data=cps)

## clustered SEs
cps1 <- na.omit(cps[ , c("electid", all.vars(formula(mod1)))])
mod1 <- cl(cps1, mod1, cps1$electid)
mod2 <- cl(cps, mod2, cps$electid)
mod3 <- cl(cps, mod3, cps$electid)

stargazer(mod1, mod2, mod3,
          omit.stat=c("f", "ser", "bic", "ll", "adj.rsq"), 
          star.cutoffs = c(0.1, 0.05, 0.01),
          column.sep.width="1pt", digits=2,
          dep.var.labels.include = FALSE,
          dep.var.caption="Outcome variable: Vote Share")